﻿
namespace AeonNexus.Crm.OCFSWorkflow
{
    using System;
    using System.Activities;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using Microsoft.Xrm.Sdk.Client;
    using System.Linq;
    using Microsoft.Xrm.Sdk.Query;
    public sealed class GetHRISDetails : CodeActivity
    {

        [Input("NYSTEP")]
        [RequiredArgument]
        [ReferenceTarget("ocfs_nystepitem")]
        public InArgument<EntityReference> NYSTEP { get; set; }

        [Output("Division")]
        [ReferenceTarget("ocfs_division")]
        public OutArgument<EntityReference> Division { get; set; }

        [Output("Bureau")]
        [ReferenceTarget("ocfs_bureau")]
        public OutArgument<EntityReference> Bureau { get; set; }

        [Output("Section")]
        [ReferenceTarget("ocfs_section")]
        public OutArgument<EntityReference> Section { get; set; }

        [Output("Location")]
        [ReferenceTarget("ocfs_location")]
        public OutArgument<EntityReference> Location { get; set; }

        [Output("URC")]
        [ReferenceTarget("ocfs_code")]
        public OutArgument<EntityReference> Urc { get; set; }

        [Output("IsHRISFound")]
        public OutArgument<bool> IsHRISFound { get; set; }


        protected override void Execute(CodeActivityContext executionContext)
        {
            // Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();
            IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
            try
            {
                IOrganizationService service = executionContext.GetExtension<IOrganizationServiceFactory>().CreateOrganizationService(context.UserId);
                // var query = new QueryByExpression()
                //var hrisRecord = (from p in ctx.CreateQuery("ocfs_hris")
                //                  where
                //                  p.GetAttributeValue<EntityReference>("ocfs_itemno").Id == NYSTEP.Get(executionContext).Id
                //                  &&
                //                  p.GetAttributeValue<OptionSetValue>("statecode").Value == 1
                //                  select new
                //                  {
                //                      HrisId = p.Id,
                //                      division = p.GetAttributeValue<EntityReference>("ocfs_division"),
                //                      bureau = p.GetAttributeValue<EntityReference>("ocfs_bureau"),
                //                      section = p.GetAttributeValue<EntityReference>("ocfs_section"),
                //                      location = p.GetAttributeValue<EntityReference>("ocfs_locationlookup"),
                //                      urc = p.GetAttributeValue<EntityReference>("ocfs_urc"),
                //                  }).FirstOrDefault();
                var query = new QueryExpression("ocfs_hris");
                query.ColumnSet.AddColumns("ocfs_hrisid", "ocfs_urc", "ocfs_section", "ocfs_locationlookup", "ocfs_location", "ocfs_division", "ocfs_bureau");
                query.Criteria.AddCondition("ocfs_itemno", ConditionOperator.Equal, NYSTEP.Get(executionContext).Id);
                var response = service.RetrieveMultiple(query);
                if (response != null && response.Entities.Count > 0)
                {
                    IsHRISFound.Set(executionContext, true);
                    tracingService.Trace("1 Count.{0}", response.Entities.Count);
                    var hrisRecord = response.Entities[0];
                    Division.Set(executionContext, hrisRecord.GetAttributeValue<EntityReference>("ocfs_division"));
                    tracingService.Trace("1 ocfs_division.{0}", hrisRecord.GetAttributeValue<EntityReference>("ocfs_division"));
                    Bureau.Set(executionContext, hrisRecord.GetAttributeValue<EntityReference>("ocfs_bureau"));
                    Section.Set(executionContext, hrisRecord.GetAttributeValue<EntityReference>("ocfs_section"));
                    Location.Set(executionContext, hrisRecord.GetAttributeValue<EntityReference>("ocfs_locationlookup"));
                    Urc.Set(executionContext, hrisRecord.GetAttributeValue<EntityReference>("ocfs_urc"));
                }
                else
                    IsHRISFound.Set(executionContext, false);
            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                tracingService.Trace("Exception: {0}", e.ToString());
                throw;
            }
            tracingService.Trace("Exiting GetHRISDetails.Execute(), Correlation Id: {0}", context.CorrelationId);
        }
    }
}